<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content=""/>

    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta name="twitter:title" content="datasets.py"/>
    <meta name="twitter:description" content=""/>
    <meta name="twitter:site" content="@labmlai"/>
    <meta name="twitter:creator" content="@labmlai"/>

    <meta property="og:url" content="https://nn.labml.ai/helpers/datasets.html"/>
    <meta property="og:title" content="datasets.py"/>
    <meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta property="og:site_name" content="datasets.py"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="datasets.py"/>
    <meta property="og:description" content=""/>

    <title>datasets.py</title>
    <link rel="shortcut icon" href="/icon.png"/>
    <link rel="stylesheet" href="../pylit.css?v=1">
    <link rel="canonical" href="https://nn.labml.ai/helpers/datasets.html"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css" integrity="sha384-zTROYFVGOfTw7JV7KUu8udsvW2fx4lWOsCEDqhBreBwlHI4ioVRtmIvEThzJHGET" crossorigin="anonymous">

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());

        gtag('config', 'G-4V3HC8HBLH');
    </script>
</head>
<body>
<div id='container'>
    <div id="background"></div>
    <div class='section'>
        <div class='docs'>
            <p>
                <a class="parent" href="/">home</a>
                <a class="parent" href="index.html">helpers</a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations" target="_blank">
                    <img alt="Github"
                         src="https://img.shields.io/github/stars/labmlai/annotated_deep_learning_paper_implementations?style=social"
                         style="max-width:100%;"/></a>
                <a href="https://twitter.com/labmlai" rel="nofollow" target="_blank">
                    <img alt="Twitter"
                         src="https://img.shields.io/twitter/follow/labmlai?style=social"
                         style="max-width:100%;"/></a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations/tree/master/labml_nn/helpers/datasets.py" target="_blank">
                    View code on Github</a>
            </p>
        </div>
    </div>
    <div class='section' id='section-0'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-0'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">1</span><span></span><span class="kn">import</span> <span class="nn">random</span>
<span class="lineno">2</span><span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">PurePath</span><span class="p">,</span> <span class="n">Path</span>
<span class="lineno">3</span><span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span><span class="p">,</span> <span class="n">Callable</span><span class="p">,</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Optional</span>
<span class="lineno">4</span>
<span class="lineno">5</span><span class="kn">from</span> <span class="nn">torchvision</span> <span class="kn">import</span> <span class="n">datasets</span><span class="p">,</span> <span class="n">transforms</span>
<span class="lineno">6</span>
<span class="lineno">7</span><span class="kn">import</span> <span class="nn">torch</span>
<span class="lineno">8</span><span class="kn">from</span> <span class="nn">labml</span> <span class="kn">import</span> <span class="n">lab</span>
<span class="lineno">9</span><span class="kn">from</span> <span class="nn">labml</span> <span class="kn">import</span> <span class="n">monit</span>
<span class="lineno">10</span><span class="kn">from</span> <span class="nn">labml.configs</span> <span class="kn">import</span> <span class="n">BaseConfigs</span>
<span class="lineno">11</span><span class="kn">from</span> <span class="nn">labml.configs</span> <span class="kn">import</span> <span class="n">aggregate</span><span class="p">,</span> <span class="n">option</span>
<span class="lineno">12</span><span class="kn">from</span> <span class="nn">labml.utils.download</span> <span class="kn">import</span> <span class="n">download_file</span>
<span class="lineno">13</span><span class="kn">from</span> <span class="nn">torch.utils.data</span> <span class="kn">import</span> <span class="n">DataLoader</span>
<span class="lineno">14</span><span class="kn">from</span> <span class="nn">torch.utils.data</span> <span class="kn">import</span> <span class="n">IterableDataset</span><span class="p">,</span> <span class="n">Dataset</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-1'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-1'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">17</span><span class="k">def</span> <span class="nf">_mnist_dataset</span><span class="p">(</span><span class="n">is_train</span><span class="p">,</span> <span class="n">transform</span><span class="p">):</span>
<span class="lineno">18</span>    <span class="k">return</span> <span class="n">datasets</span><span class="o">.</span><span class="n">MNIST</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">lab</span><span class="o">.</span><span class="n">get_data_path</span><span class="p">()),</span>
<span class="lineno">19</span>                          <span class="n">train</span><span class="o">=</span><span class="n">is_train</span><span class="p">,</span>
<span class="lineno">20</span>                          <span class="n">download</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="lineno">21</span>                          <span class="n">transform</span><span class="o">=</span><span class="n">transform</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-2'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-2'>#</a>
            </div>
            <p> Configurable MNIST data set.</p>
<p>Arguments:  dataset_name (str): name of the data set, <code  class="highlight"><span></span></code>
MNIST<code  class="highlight"><span></span></code>
  dataset_transforms (torchvision.transforms.Compose): image transformations  train_dataset (torchvision.datasets.MNIST): training dataset  valid_dataset (torchvision.datasets.MNIST): validation dataset</p>
<p> train_loader (torch.utils.data.DataLoader): training data loader  valid_loader (torch.utils.data.DataLoader): validation data loader</p>
<p> train_batch_size (int): training batch size  valid_batch_size (int): validation batch size</p>
<p> train_loader_shuffle (bool): whether to shuffle training data  valid_loader_shuffle (bool): whether to shuffle validation data</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">24</span><span class="k">class</span> <span class="nc">MNISTConfigs</span><span class="p">(</span><span class="n">BaseConfigs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-3'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-3'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">44</span>    <span class="n">dataset_name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;MNIST&#39;</span>
<span class="lineno">45</span>    <span class="n">dataset_transforms</span><span class="p">:</span> <span class="n">transforms</span><span class="o">.</span><span class="n">Compose</span>
<span class="lineno">46</span>    <span class="n">train_dataset</span><span class="p">:</span> <span class="n">datasets</span><span class="o">.</span><span class="n">MNIST</span>
<span class="lineno">47</span>    <span class="n">valid_dataset</span><span class="p">:</span> <span class="n">datasets</span><span class="o">.</span><span class="n">MNIST</span>
<span class="lineno">48</span>
<span class="lineno">49</span>    <span class="n">train_loader</span><span class="p">:</span> <span class="n">DataLoader</span>
<span class="lineno">50</span>    <span class="n">valid_loader</span><span class="p">:</span> <span class="n">DataLoader</span>
<span class="lineno">51</span>
<span class="lineno">52</span>    <span class="n">train_batch_size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">64</span>
<span class="lineno">53</span>    <span class="n">valid_batch_size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1024</span>
<span class="lineno">54</span>
<span class="lineno">55</span>    <span class="n">train_loader_shuffle</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span>
<span class="lineno">56</span>    <span class="n">valid_loader_shuffle</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-4'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-4'>#</a>
            </div>
            <p> Configurable CIFAR 10 data set.</p>
<p>Arguments:  dataset_name (str): name of the data set, <code  class="highlight"><span></span></code>
CIFAR10<code  class="highlight"><span></span></code>
  dataset_transforms (torchvision.transforms.Compose): image transformations  train_dataset (torchvision.datasets.CIFAR10): training dataset  valid_dataset (torchvision.datasets.CIFAR10): validation dataset</p>
<p> train_loader (torch.utils.data.DataLoader): training data loader  valid_loader (torch.utils.data.DataLoader): validation data loader</p>
<p> train_batch_size (int): training batch size  valid_batch_size (int): validation batch size</p>
<p> train_loader_shuffle (bool): whether to shuffle training data  valid_loader_shuffle (bool): whether to shuffle validation data</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">59</span><span class="nd">@option</span><span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">)</span>
<span class="lineno">60</span><span class="k">def</span> <span class="nf">mnist_transforms</span><span class="p">():</span>
<span class="lineno">61</span>    <span class="k">return</span> <span class="n">transforms</span><span class="o">.</span><span class="n">Compose</span><span class="p">([</span>
<span class="lineno">62</span>        <span class="n">transforms</span><span class="o">.</span><span class="n">ToTensor</span><span class="p">(),</span>
<span class="lineno">63</span>        <span class="n">transforms</span><span class="o">.</span><span class="n">Normalize</span><span class="p">((</span><span class="mf">0.1307</span><span class="p">,),</span> <span class="p">(</span><span class="mf">0.3081</span><span class="p">,))</span>
<span class="lineno">64</span>    <span class="p">])</span>
<span class="lineno">65</span>
<span class="lineno">66</span>
<span class="lineno">67</span><span class="nd">@option</span><span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">train_dataset</span><span class="p">)</span>
<span class="lineno">68</span><span class="k">def</span> <span class="nf">mnist_train_dataset</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">MNISTConfigs</span><span class="p">):</span>
<span class="lineno">69</span>    <span class="k">return</span> <span class="n">_mnist_dataset</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">)</span>
<span class="lineno">70</span>
<span class="lineno">71</span>
<span class="lineno">72</span><span class="nd">@option</span><span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">valid_dataset</span><span class="p">)</span>
<span class="lineno">73</span><span class="k">def</span> <span class="nf">mnist_valid_dataset</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">MNISTConfigs</span><span class="p">):</span>
<span class="lineno">74</span>    <span class="k">return</span> <span class="n">_mnist_dataset</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">)</span>
<span class="lineno">75</span>
<span class="lineno">76</span>
<span class="lineno">77</span><span class="nd">@option</span><span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">train_loader</span><span class="p">)</span>
<span class="lineno">78</span><span class="k">def</span> <span class="nf">mnist_train_loader</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">MNISTConfigs</span><span class="p">):</span>
<span class="lineno">79</span>    <span class="k">return</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">train_dataset</span><span class="p">,</span>
<span class="lineno">80</span>                      <span class="n">batch_size</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">train_batch_size</span><span class="p">,</span>
<span class="lineno">81</span>                      <span class="n">shuffle</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">train_loader_shuffle</span><span class="p">)</span>
<span class="lineno">82</span>
<span class="lineno">83</span>
<span class="lineno">84</span><span class="nd">@option</span><span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">valid_loader</span><span class="p">)</span>
<span class="lineno">85</span><span class="k">def</span> <span class="nf">mnist_valid_loader</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">MNISTConfigs</span><span class="p">):</span>
<span class="lineno">86</span>    <span class="k">return</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">valid_dataset</span><span class="p">,</span>
<span class="lineno">87</span>                      <span class="n">batch_size</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">valid_batch_size</span><span class="p">,</span>
<span class="lineno">88</span>                      <span class="n">shuffle</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">valid_loader_shuffle</span><span class="p">)</span>
<span class="lineno">89</span>
<span class="lineno">90</span>
<span class="lineno">91</span><span class="n">aggregate</span><span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">dataset_name</span><span class="p">,</span> <span class="s1">&#39;MNIST&#39;</span><span class="p">,</span>
<span class="lineno">92</span>          <span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">,</span> <span class="s1">&#39;mnist_transforms&#39;</span><span class="p">),</span>
<span class="lineno">93</span>          <span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">train_dataset</span><span class="p">,</span> <span class="s1">&#39;mnist_train_dataset&#39;</span><span class="p">),</span>
<span class="lineno">94</span>          <span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">valid_dataset</span><span class="p">,</span> <span class="s1">&#39;mnist_valid_dataset&#39;</span><span class="p">),</span>
<span class="lineno">95</span>          <span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">train_loader</span><span class="p">,</span> <span class="s1">&#39;mnist_train_loader&#39;</span><span class="p">),</span>
<span class="lineno">96</span>          <span class="p">(</span><span class="n">MNISTConfigs</span><span class="o">.</span><span class="n">valid_loader</span><span class="p">,</span> <span class="s1">&#39;mnist_valid_loader&#39;</span><span class="p">))</span>
<span class="lineno">97</span>
<span class="lineno">98</span>
<span class="lineno">99</span><span class="k">def</span> <span class="nf">_cifar_dataset</span><span class="p">(</span><span class="n">is_train</span><span class="p">,</span> <span class="n">transform</span><span class="p">):</span>
<span class="lineno">100</span>    <span class="k">return</span> <span class="n">datasets</span><span class="o">.</span><span class="n">CIFAR10</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">lab</span><span class="o">.</span><span class="n">get_data_path</span><span class="p">()),</span>
<span class="lineno">101</span>                            <span class="n">train</span><span class="o">=</span><span class="n">is_train</span><span class="p">,</span>
<span class="lineno">102</span>                            <span class="n">download</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="lineno">103</span>                            <span class="n">transform</span><span class="o">=</span><span class="n">transform</span><span class="p">)</span>
<span class="lineno">104</span>
<span class="lineno">105</span>
<span class="lineno">106</span><span class="k">class</span> <span class="nc">CIFAR10Configs</span><span class="p">(</span><span class="n">BaseConfigs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-5'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-5'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">125</span>    <span class="n">dataset_name</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s1">&#39;CIFAR10&#39;</span>
<span class="lineno">126</span>    <span class="n">dataset_transforms</span><span class="p">:</span> <span class="n">transforms</span><span class="o">.</span><span class="n">Compose</span>
<span class="lineno">127</span>    <span class="n">train_dataset</span><span class="p">:</span> <span class="n">datasets</span><span class="o">.</span><span class="n">CIFAR10</span>
<span class="lineno">128</span>    <span class="n">valid_dataset</span><span class="p">:</span> <span class="n">datasets</span><span class="o">.</span><span class="n">CIFAR10</span>
<span class="lineno">129</span>
<span class="lineno">130</span>    <span class="n">train_loader</span><span class="p">:</span> <span class="n">DataLoader</span>
<span class="lineno">131</span>    <span class="n">valid_loader</span><span class="p">:</span> <span class="n">DataLoader</span>
<span class="lineno">132</span>
<span class="lineno">133</span>    <span class="n">train_batch_size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">64</span>
<span class="lineno">134</span>    <span class="n">valid_batch_size</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="mi">1024</span>
<span class="lineno">135</span>
<span class="lineno">136</span>    <span class="n">train_loader_shuffle</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span>
<span class="lineno">137</span>    <span class="n">valid_loader_shuffle</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-6'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-6'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">140</span><span class="nd">@CIFAR10Configs</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">)</span>
<span class="lineno">141</span><span class="k">def</span> <span class="nf">cifar10_transforms</span><span class="p">():</span>
<span class="lineno">142</span>    <span class="k">return</span> <span class="n">transforms</span><span class="o">.</span><span class="n">Compose</span><span class="p">([</span>
<span class="lineno">143</span>        <span class="n">transforms</span><span class="o">.</span><span class="n">ToTensor</span><span class="p">(),</span>
<span class="lineno">144</span>        <span class="n">transforms</span><span class="o">.</span><span class="n">Normalize</span><span class="p">((</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">),</span> <span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">))</span>
<span class="lineno">145</span>    <span class="p">])</span>
<span class="lineno">146</span>
<span class="lineno">147</span>
<span class="lineno">148</span><span class="nd">@CIFAR10Configs</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">train_dataset</span><span class="p">)</span>
<span class="lineno">149</span><span class="k">def</span> <span class="nf">cifar10_train_dataset</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">CIFAR10Configs</span><span class="p">):</span>
<span class="lineno">150</span>    <span class="k">return</span> <span class="n">_cifar_dataset</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">)</span>
<span class="lineno">151</span>
<span class="lineno">152</span>
<span class="lineno">153</span><span class="nd">@CIFAR10Configs</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">valid_dataset</span><span class="p">)</span>
<span class="lineno">154</span><span class="k">def</span> <span class="nf">cifar10_valid_dataset</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">CIFAR10Configs</span><span class="p">):</span>
<span class="lineno">155</span>    <span class="k">return</span> <span class="n">_cifar_dataset</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="n">c</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">)</span>
<span class="lineno">156</span>
<span class="lineno">157</span>
<span class="lineno">158</span><span class="nd">@CIFAR10Configs</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">train_loader</span><span class="p">)</span>
<span class="lineno">159</span><span class="k">def</span> <span class="nf">cifar10_train_loader</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">CIFAR10Configs</span><span class="p">):</span>
<span class="lineno">160</span>    <span class="k">return</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">train_dataset</span><span class="p">,</span>
<span class="lineno">161</span>                      <span class="n">batch_size</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">train_batch_size</span><span class="p">,</span>
<span class="lineno">162</span>                      <span class="n">shuffle</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">train_loader_shuffle</span><span class="p">)</span>
<span class="lineno">163</span>
<span class="lineno">164</span>
<span class="lineno">165</span><span class="nd">@CIFAR10Configs</span><span class="o">.</span><span class="n">calc</span><span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">valid_loader</span><span class="p">)</span>
<span class="lineno">166</span><span class="k">def</span> <span class="nf">cifar10_valid_loader</span><span class="p">(</span><span class="n">c</span><span class="p">:</span> <span class="n">CIFAR10Configs</span><span class="p">):</span>
<span class="lineno">167</span>    <span class="k">return</span> <span class="n">DataLoader</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">valid_dataset</span><span class="p">,</span>
<span class="lineno">168</span>                      <span class="n">batch_size</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">valid_batch_size</span><span class="p">,</span>
<span class="lineno">169</span>                      <span class="n">shuffle</span><span class="o">=</span><span class="n">c</span><span class="o">.</span><span class="n">valid_loader_shuffle</span><span class="p">)</span>
<span class="lineno">170</span>
<span class="lineno">171</span>
<span class="lineno">172</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">aggregate</span><span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">dataset_name</span><span class="p">,</span> <span class="s1">&#39;CIFAR10&#39;</span><span class="p">,</span>
<span class="lineno">173</span>                         <span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">dataset_transforms</span><span class="p">,</span> <span class="s1">&#39;cifar10_transforms&#39;</span><span class="p">),</span>
<span class="lineno">174</span>                         <span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">train_dataset</span><span class="p">,</span> <span class="s1">&#39;cifar10_train_dataset&#39;</span><span class="p">),</span>
<span class="lineno">175</span>                         <span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">valid_dataset</span><span class="p">,</span> <span class="s1">&#39;cifar10_valid_dataset&#39;</span><span class="p">),</span>
<span class="lineno">176</span>                         <span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">train_loader</span><span class="p">,</span> <span class="s1">&#39;cifar10_train_loader&#39;</span><span class="p">),</span>
<span class="lineno">177</span>                         <span class="p">(</span><span class="n">CIFAR10Configs</span><span class="o">.</span><span class="n">valid_loader</span><span class="p">,</span> <span class="s1">&#39;cifar10_valid_loader&#39;</span><span class="p">))</span>
<span class="lineno">178</span>
<span class="lineno">179</span>
<span class="lineno">180</span><span class="k">class</span> <span class="nc">TextDataset</span><span class="p">:</span>
<span class="lineno">181</span>    <span class="n">itos</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span>
<span class="lineno">182</span>    <span class="n">stoi</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span>
<span class="lineno">183</span>    <span class="n">n_tokens</span><span class="p">:</span> <span class="nb">int</span>
<span class="lineno">184</span>    <span class="n">train</span><span class="p">:</span> <span class="nb">str</span>
<span class="lineno">185</span>    <span class="n">valid</span><span class="p">:</span> <span class="nb">str</span>
<span class="lineno">186</span>    <span class="n">standard_tokens</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
<span class="lineno">187</span>
<span class="lineno">188</span>    <span class="nd">@staticmethod</span>
<span class="lineno">189</span>    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="n">path</span><span class="p">:</span> <span class="n">PurePath</span><span class="p">):</span>
<span class="lineno">190</span>        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">path</span><span class="p">),</span> <span class="s1">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="lineno">191</span>            <span class="k">return</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="lineno">192</span>
<span class="lineno">193</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="n">PurePath</span><span class="p">,</span> <span class="n">tokenizer</span><span class="p">:</span> <span class="n">Callable</span><span class="p">,</span> <span class="n">train</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">valid</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">test</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span>
<span class="lineno">194</span>                 <span class="n">n_tokens</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="lineno">195</span>                 <span class="n">stoi</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="nb">int</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="lineno">196</span>                 <span class="n">itos</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="lineno">197</span>        <span class="bp">self</span><span class="o">.</span><span class="n">test</span> <span class="o">=</span> <span class="n">test</span>
<span class="lineno">198</span>        <span class="bp">self</span><span class="o">.</span><span class="n">valid</span> <span class="o">=</span> <span class="n">valid</span>
<span class="lineno">199</span>        <span class="bp">self</span><span class="o">.</span><span class="n">train</span> <span class="o">=</span> <span class="n">train</span>
<span class="lineno">200</span>        <span class="bp">self</span><span class="o">.</span><span class="n">tokenizer</span> <span class="o">=</span> <span class="n">tokenizer</span>
<span class="lineno">201</span>        <span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">path</span>
<span class="lineno">202</span>
<span class="lineno">203</span>        <span class="k">if</span> <span class="n">n_tokens</span> <span class="ow">or</span> <span class="n">stoi</span> <span class="ow">or</span> <span class="n">itos</span><span class="p">:</span>
<span class="lineno">204</span>            <span class="k">assert</span> <span class="n">stoi</span> <span class="ow">and</span> <span class="n">itos</span> <span class="ow">and</span> <span class="n">n_tokens</span>
<span class="lineno">205</span>            <span class="bp">self</span><span class="o">.</span><span class="n">n_tokens</span> <span class="o">=</span> <span class="n">n_tokens</span>
<span class="lineno">206</span>            <span class="bp">self</span><span class="o">.</span><span class="n">stoi</span> <span class="o">=</span> <span class="n">stoi</span>
<span class="lineno">207</span>            <span class="bp">self</span><span class="o">.</span><span class="n">itos</span> <span class="o">=</span> <span class="n">itos</span>
<span class="lineno">208</span>        <span class="k">else</span><span class="p">:</span>
<span class="lineno">209</span>            <span class="bp">self</span><span class="o">.</span><span class="n">n_tokens</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">standard_tokens</span><span class="p">)</span>
<span class="lineno">210</span>            <span class="bp">self</span><span class="o">.</span><span class="n">stoi</span> <span class="o">=</span> <span class="p">{</span><span class="n">t</span><span class="p">:</span> <span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">standard_tokens</span><span class="p">)}</span>
<span class="lineno">211</span>
<span class="lineno">212</span>            <span class="k">with</span> <span class="n">monit</span><span class="o">.</span><span class="n">section</span><span class="p">(</span><span class="s2">&quot;Tokenize&quot;</span><span class="p">):</span>
<span class="lineno">213</span>                <span class="n">tokens</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tokenizer</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">train</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">tokenizer</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">valid</span><span class="p">)</span>
<span class="lineno">214</span>                <span class="n">tokens</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">tokens</span><span class="p">)))</span>
<span class="lineno">215</span>
<span class="lineno">216</span>            <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">monit</span><span class="o">.</span><span class="n">iterate</span><span class="p">(</span><span class="s2">&quot;Build vocabulary&quot;</span><span class="p">,</span> <span class="n">tokens</span><span class="p">):</span>
<span class="lineno">217</span>                <span class="bp">self</span><span class="o">.</span><span class="n">stoi</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">n_tokens</span>
<span class="lineno">218</span>                <span class="bp">self</span><span class="o">.</span><span class="n">n_tokens</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="lineno">219</span>
<span class="lineno">220</span>            <span class="bp">self</span><span class="o">.</span><span class="n">itos</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;&#39;</span><span class="p">]</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">n_tokens</span>
<span class="lineno">221</span>            <span class="k">for</span> <span class="n">t</span><span class="p">,</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stoi</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="lineno">222</span>                <span class="bp">self</span><span class="o">.</span><span class="n">itos</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="o">=</span> <span class="n">t</span>
<span class="lineno">223</span>
<span class="lineno">224</span>    <span class="k">def</span> <span class="nf">text_to_i</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">torch</span><span class="o">.</span><span class="n">Tensor</span><span class="p">:</span>
<span class="lineno">225</span>        <span class="n">tokens</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tokenizer</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
<span class="lineno">226</span>        <span class="k">return</span> <span class="n">torch</span><span class="o">.</span><span class="n">tensor</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">stoi</span><span class="p">[</span><span class="n">s</span><span class="p">]</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">tokens</span> <span class="k">if</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">stoi</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">torch</span><span class="o">.</span><span class="n">long</span><span class="p">)</span>
<span class="lineno">227</span>
<span class="lineno">228</span>    <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="lineno">229</span>        <span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">train</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">1_000_000</span><span class="w"> </span><span class="si">:</span><span class="s1">,.2f</span><span class="si">}</span><span class="s1">M, </span><span class="si">{</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">valid</span><span class="p">)</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">1_000_000</span><span class="w"> </span><span class="si">:</span><span class="s1">,.2f</span><span class="si">}</span><span class="s1">M - </span><span class="si">{</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">)</span><span class="si">}</span><span class="s1">&#39;</span>
<span class="lineno">230</span>
<span class="lineno">231</span>
<span class="lineno">232</span><span class="k">class</span> <span class="nc">SequentialDataLoader</span><span class="p">(</span><span class="n">IterableDataset</span><span class="p">):</span>
<span class="lineno">233</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">dataset</span><span class="p">:</span> <span class="n">TextDataset</span><span class="p">,</span>
<span class="lineno">234</span>                 <span class="n">batch_size</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">seq_len</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
<span class="lineno">235</span>        <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span> <span class="o">=</span> <span class="n">seq_len</span>
<span class="lineno">236</span>        <span class="n">data</span> <span class="o">=</span> <span class="n">dataset</span><span class="o">.</span><span class="n">text_to_i</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
<span class="lineno">237</span>        <span class="n">n_batch</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">//</span> <span class="n">batch_size</span>
<span class="lineno">238</span>        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">narrow</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">n_batch</span> <span class="o">*</span> <span class="n">batch_size</span><span class="p">)</span>
<span class="lineno">239</span>        <span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">t</span><span class="p">()</span><span class="o">.</span><span class="n">contiguous</span><span class="p">()</span>
<span class="lineno">240</span>        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
<span class="lineno">241</span>
<span class="lineno">242</span>    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="lineno">243</span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">//</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span>
<span class="lineno">244</span>
<span class="lineno">245</span>    <span class="k">def</span> <span class="fm">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="lineno">246</span>        <span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">=</span> <span class="mi">0</span>
<span class="lineno">247</span>        <span class="k">return</span> <span class="bp">self</span>
<span class="lineno">248</span>
<span class="lineno">249</span>    <span class="k">def</span> <span class="fm">__next__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="lineno">250</span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span>
<span class="lineno">251</span>            <span class="k">raise</span> <span class="ne">StopIteration</span><span class="p">()</span>
<span class="lineno">252</span>
<span class="lineno">253</span>        <span class="n">seq_len</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">)</span>
<span class="lineno">254</span>        <span class="n">i</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">+</span> <span class="n">seq_len</span>
<span class="lineno">255</span>        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span><span class="p">:</span> <span class="n">i</span><span class="p">]</span>
<span class="lineno">256</span>        <span class="n">target</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
<span class="lineno">257</span>        <span class="bp">self</span><span class="o">.</span><span class="n">idx</span> <span class="o">=</span> <span class="n">i</span>
<span class="lineno">258</span>        <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">target</span>
<span class="lineno">259</span>
<span class="lineno">260</span>    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">idx</span><span class="p">):</span>
<span class="lineno">261</span>        <span class="n">seq_len</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">idx</span><span class="p">)</span>
<span class="lineno">262</span>        <span class="n">i</span> <span class="o">=</span> <span class="n">idx</span> <span class="o">+</span> <span class="n">seq_len</span>
<span class="lineno">263</span>        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">idx</span><span class="p">:</span> <span class="n">i</span><span class="p">]</span>
<span class="lineno">264</span>        <span class="n">target</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">idx</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
<span class="lineno">265</span>        <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">target</span>
<span class="lineno">266</span>
<span class="lineno">267</span>
<span class="lineno">268</span><span class="k">class</span> <span class="nc">SequentialUnBatchedDataset</span><span class="p">(</span><span class="n">Dataset</span><span class="p">):</span>
<span class="lineno">269</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">dataset</span><span class="p">:</span> <span class="n">TextDataset</span><span class="p">,</span>
<span class="lineno">270</span>                 <span class="n">seq_len</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
<span class="lineno">271</span>                 <span class="n">is_random_offset</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">):</span>
<span class="lineno">272</span>        <span class="bp">self</span><span class="o">.</span><span class="n">is_random_offset</span> <span class="o">=</span> <span class="n">is_random_offset</span>
<span class="lineno">273</span>        <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span> <span class="o">=</span> <span class="n">seq_len</span>
<span class="lineno">274</span>        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">dataset</span><span class="o">.</span><span class="n">text_to_i</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
<span class="lineno">275</span>
<span class="lineno">276</span>    <span class="k">def</span> <span class="fm">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="lineno">277</span>        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">//</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span>
<span class="lineno">278</span>
<span class="lineno">279</span>    <span class="k">def</span> <span class="fm">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">idx</span><span class="p">):</span>
<span class="lineno">280</span>        <span class="n">start</span> <span class="o">=</span> <span class="n">idx</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span>
<span class="lineno">281</span>        <span class="k">assert</span> <span class="n">start</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="lineno">282</span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_random_offset</span><span class="p">:</span>
<span class="lineno">283</span>            <span class="n">start</span> <span class="o">+=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">min</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="p">(</span><span class="n">start</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)))</span>
<span class="lineno">284</span>
<span class="lineno">285</span>        <span class="n">end</span> <span class="o">=</span> <span class="n">start</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">seq_len</span>
<span class="lineno">286</span>        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">start</span><span class="p">:</span> <span class="n">end</span><span class="p">]</span>
<span class="lineno">287</span>        <span class="n">target</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="n">start</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:</span> <span class="n">end</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
<span class="lineno">288</span>        <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">target</span>
<span class="lineno">289</span>
<span class="lineno">290</span>
<span class="lineno">291</span><span class="k">class</span> <span class="nc">TextFileDataset</span><span class="p">(</span><span class="n">TextDataset</span><span class="p">):</span>
<span class="lineno">292</span>    <span class="n">standard_tokens</span> <span class="o">=</span> <span class="p">[]</span>
<span class="lineno">293</span>
<span class="lineno">294</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="n">PurePath</span><span class="p">,</span> <span class="n">tokenizer</span><span class="p">:</span> <span class="n">Callable</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span>
<span class="lineno">295</span>                 <span class="n">url</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="lineno">296</span>                 <span class="n">filter_subset</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="lineno">297</span>        <span class="n">path</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="lineno">298</span>        <span class="k">if</span> <span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
<span class="lineno">299</span>            <span class="k">if</span> <span class="ow">not</span> <span class="n">url</span><span class="p">:</span>
<span class="lineno">300</span>                <span class="k">raise</span> <span class="ne">FileNotFoundError</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
<span class="lineno">301</span>            <span class="k">else</span><span class="p">:</span>
<span class="lineno">302</span>                <span class="n">download_file</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">path</span><span class="p">)</span>
<span class="lineno">303</span>
<span class="lineno">304</span>        <span class="k">with</span> <span class="n">monit</span><span class="o">.</span><span class="n">section</span><span class="p">(</span><span class="s2">&quot;Load data&quot;</span><span class="p">):</span>
<span class="lineno">305</span>            <span class="n">text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="lineno">306</span>            <span class="k">if</span> <span class="n">filter_subset</span><span class="p">:</span>
<span class="lineno">307</span>                <span class="n">text</span> <span class="o">=</span> <span class="n">text</span><span class="p">[:</span><span class="n">filter_subset</span><span class="p">]</span>
<span class="lineno">308</span>            <span class="n">split</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">text</span><span class="p">)</span> <span class="o">*</span> <span class="mf">.9</span><span class="p">)</span>
<span class="lineno">309</span>            <span class="n">train</span> <span class="o">=</span> <span class="n">text</span><span class="p">[:</span><span class="n">split</span><span class="p">]</span>
<span class="lineno">310</span>            <span class="n">valid</span> <span class="o">=</span> <span class="n">text</span><span class="p">[</span><span class="n">split</span><span class="p">:]</span>
<span class="lineno">311</span>
<span class="lineno">312</span>        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">tokenizer</span><span class="p">,</span> <span class="n">train</span><span class="p">,</span> <span class="n">valid</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
<span class="lineno">313</span>
<span class="lineno">314</span>
<span class="lineno">315</span><span class="k">def</span> <span class="nf">_test_tiny_shakespeare</span><span class="p">():</span>
<span class="lineno">316</span>    <span class="kn">from</span> <span class="nn">labml</span> <span class="kn">import</span> <span class="n">lab</span>
<span class="lineno">317</span>    <span class="n">_</span> <span class="o">=</span> <span class="n">TextFileDataset</span><span class="p">(</span><span class="n">lab</span><span class="o">.</span><span class="n">get_data_path</span><span class="p">()</span> <span class="o">/</span> <span class="s1">&#39;tiny_shakespeare.txt&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="n">x</span><span class="p">),</span>
<span class="lineno">318</span>                        <span class="n">url</span><span class="o">=</span><span class="s1">&#39;https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt&#39;</span><span class="p">)</span>
<span class="lineno">319</span>
<span class="lineno">320</span>
<span class="lineno">321</span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
<span class="lineno">322</span>    <span class="n">_test_tiny_shakespeare</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='footer'>
        <a href="https://labml.ai">labml.ai</a>
    </div>
</div>
<script src=../interactive.js?v=1"></script>
<script>
    function handleImages() {
        var images = document.querySelectorAll('p>img')

        for (var i = 0; i < images.length; ++i) {
            handleImage(images[i])
        }
    }

    function handleImage(img) {
        img.parentElement.style.textAlign = 'center'

        var modal = document.createElement('div')
        modal.id = 'modal'

        var modalContent = document.createElement('div')
        modal.appendChild(modalContent)

        var modalImage = document.createElement('img')
        modalContent.appendChild(modalImage)

        var span = document.createElement('span')
        span.classList.add('close')
        span.textContent = 'x'
        modal.appendChild(span)

        img.onclick = function () {
            console.log('clicked')
            document.body.appendChild(modal)
            modalImage.src = img.src
        }

        span.onclick = function () {
            document.body.removeChild(modal)
        }
    }

    handleImages()
</script>
</body>
</html>